package gases.as3.librarys.components.ui.control.group
{
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.components.events.GButtonEvent;
	import gases.as3.librarys.components.events.GButtonGroupEvent;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.control.GButton;
	
	public class GButtonGroup extends GUIComponents
	{
		
	
		public function GButtonGroup()
		{
			super();
		}
		
		override protected function initialization():void
		{
			_btns = new Array();
			_btnDic = new Dictionary();
		}
		
		/**
		 * 增加一个ID 
		 * @param btn
		 * 
		 */		
		public function addButton(btn:GButton):void
		{
			_btns.push(btn.id);
			_btnDic[btn.id] = btn;
			setButtonProperty(btn);
			this.addChild(btn);
		}
		
		/**
		 * 删除按钮 
		 * @param id
		 * 
		 */		
		public function deleteButton(id:String):void
		{
			var index:int = _btns.indexOf(id);
			if(index != -1)
			{
				_btns.splice(index, 1);
			}
			if(_btnDic[id])
			{
				_btnDic[id] = null;
				delete _btnDic[id];
			}
		}
		
		/**
		 *通过id查找button 
		 * @param id
		 * @return 
		 * 
		 */		
		public function selectButtonById(id:String):GButton
		{
			var ret:GButton;
			var index:int = _btns.indexOf(id);
			if(index != -1)
			{
				ret = _btnDic[id];
			}
			return ret;
		}
		
		/**
		 * 获取按钮数量 
		 * @return 
		 * 
		 */		
		public function get btnNum():int
		{
			return _btns.length;
		}
		
		private function setButtonProperty(btn:GButton):void
		{
			btn.isSelect = true;
			btn.selectState = false;
			btn.addEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
			btn.addEventListener(GButtonEvent.BUTTON_UNSELECT, onButtonUnSelect);
		}
		
		private function removeButtonPorperty(btn:GButton):void
		{
			btn.isSelect = false;
			btn.selectState = false;
			btn.removeEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
			btn.removeEventListener(GButtonEvent.BUTTON_UNSELECT, onButtonUnSelect);
		}
		
		private function onButtonSelect(e:GButtonEvent):void
		{
			var btn:GButton = e.target as GButton;
			for each(var button:GButton in _btnDic)
			{
				if(btn.id == button.id)
				{
					button.removeEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
					button.selectState = true;
					button.enable = false;
					this.dispatchEvent(new GButtonGroupEvent(GButtonGroupEvent.BUTTON_CLICLK,btn.id));
				}
				else
				{
					button.addEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
					button.selectState = false;
					button.enable = true;
				}
			}
		}
		private function onButtonUnSelect(e:GButtonEvent):void
		{
		}
		
		override public function dispose():void
		{
			for each(var btn:GButton in _btnDic)
			{
				removeButtonPorperty(btn);	
			}
			_btnDic = null;
			_btns = null;
			super.dispose();
		}
		
		private var _btns:Array;
		private var _btnNum:uint = 0;
		private var _btnDic:Dictionary;
	}
}
